<template>
  <div>
    <form-marker 
      :formData="list" 
      :rulesList="rules"
      :selectList="selectData"
      :eidtList="detailData" 
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker>
  </div>
</template>
<script>
import FormMarker from '@/components/createPage/createMarker'

export default {
  components: {
    FormMarker
  },
  data () {
    let onCheckName = (rule, value, callback) => {
      if (value && this.pageType === 'new') {
        this.$http.post(`/${this.$const.PREFIX.MAINDATA}/morganizations/check`, {name: value}).then((res) => {
          if (res.result) {
            // this.onCheckName = res.data // res.data.data [false]有重复/【true】无重复
            if (!res.data) {
              callback(new Error('名称已存在，请重新输入'))
            } else {
              callback()
            }
          }
        })
      } else {
        callback()
      }
    }
    return {
      list: [
        {
          type: 'input',
          label: '部门名称',
          name: '部门名称',
          prop: 'name'
        },
        {
          type: 'select',
          label: '上级部门',
          name: '上级部门',
          prop: 'parentCode',
          propName: 'parentName',
          optionLabel: 'name',
          optionValue: 'code'
        }
      ],
      rules: {
        name: [
          { validator: onCheckName, trigger: 'change' },
          { max: 32, message: '长度不可超过32个字符', trigger: 'blur' },
          { required: true, message: '部门名称不允许为空', trigger: 'blur' }
        ]
      },
      selectData: {},
      detailData: {}
    }
  },
  mounted () {
    this.pageType = this.$route.params.id
    this.getDepartmentSelect()
    this.getDepartmentDetail()
  },
  methods: {
    // 获取下拉列表数据
    async getDepartmentSelect () {
      let curId = this.pageType === 'new' ? null : this.pageType
      let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/morganizations/list`, {id: curId}) // 上级分类 parentCode: -1
      this.selectData.parentCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取编辑详情数据
    async getDepartmentDetail () {
      if (this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/morganizations/getOne`, {id: this.pageType})
        this.detailData = data
      }
    },
    // 接口保存新建信息
    onSaveInfo (val) {
      let url = this.pageType === 'new' ? `/${this.$const.PREFIX.MAINDATA}/morganizations/save` : `/${this.$const.PREFIX.MAINDATA}/morganizations/update`
      let params = val
      if (!params.name && !params.parentCode) {
        this.$message.error('请填写新增信息')
        return
      }
      this.$http.post(url, params).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'framework'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'framework'})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>